// 这份代码专门处理逻辑层的操作
const interviewDao = require('../dao/interviewDao');
const fileDao = require('../dao/fileDao');

const powerService = require('./powerService');

const interviewService = {
    /*
     * 查询所有访谈记录
     */
    async getAllInterviewList(uid, userType) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '查看访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        let func = (userType == '辅导员' || userType == '学院书记') ? interviewDao.getAllInterviewListOfSelfFaculty : interviewDao.getAllInterviewList;
        try {
            result = await func(uid);
            result = { success: true, msg: "查询成功", result };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，查询失败" };
        }
        return result;
    },
    /*
     * 新增日常访谈记录接口
     */
    async addDailyVisitInterview(uid, studentId, startTime, endTime, teacherId, interviewContent, isWarning, interviewPhoto) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.addDailyVisitInterview(studentId, startTime, endTime, teacherId, interviewContent, isWarning, interviewPhoto);
            result = { success: true, msg: "新增成功", newInterviewId: result.insertId };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，新增失败" };
        }
        return result;
    },
    async addDailyVisitInterviewNoImg(uid, studentId, startTime, endTime, teacherId, interviewContent, isWarning) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.addDailyVisitInterviewNoImg(studentId, startTime, endTime, teacherId, interviewContent, isWarning);
            result = { success: true, msg: "新增成功", newInterviewId: result.insertId };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，新增失败" };
        }
        return result;
    },
    /*
     * 新增普查访谈记录
     */
    async addCensusVisitInterview(uid, studentId, startTime, endTime, counselorId, question1, question2, question3, question4, question5, question6, question7, note, centerOpinionId, warningLevelId) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.addCensusVisitInterview(studentId, startTime, endTime, counselorId, question1, question2, question3, question4, question5, question6, question7, note, centerOpinionId, warningLevelId);
            result = { success: true, msg: "新增成功", newInterviewId: result.insertId };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，新增失败" };
        }
        return result;
    },
    /*
     * 新增家校沟通访谈记录
     */
    async addHomeSchoolInterview(uid, studentId, startTime, endTime, teacherId, interviewContent, isWarning, interviewPhoto) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.addHomeSchoolInterview(studentId, startTime, endTime, teacherId, interviewContent, isWarning, interviewPhoto);
            result = { success: true, msg: "新增成功", newInterviewId: result.insertId };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，新增失败" };
        }
        return result;
    },
    async addHomeSchoolInterviewNoImg(uid, studentId, startTime, endTime, teacherId, interviewContent, isWarning) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.addHomeSchoolInterviewNoImg(studentId, startTime, endTime, teacherId, interviewContent, isWarning);
            result = { success: true, msg: "新增成功", newInterviewId: result.insertId };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，新增失败" };
        }
        return result;
    },
    /*
     * 修改日常访谈记录接口
     */
    async updateDailyInterview(uid, userType, interviewId, startTime, teacherId, interviewContent, isWarning) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.updateDailyInterview(interviewId, startTime, teacherId, interviewContent, isWarning);
            result = { success: true, msg: "修改成功", changedRows: result.changedRows };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，修改失败" };
        }
        return result;
    },
    /*
     * 修改普查访谈记录接口
     */
    async updateCensusInterview(uid, userType, interviewId, startTime, counselorId, question1, question2, question3, question4, question5, question6, question7, note, centerOpinionId, warningLevelId) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.updateCensusInterview(interviewId, startTime, counselorId, question1, question2, question3, question4, question5, question6, question7, note, centerOpinionId, warningLevelId);
            result = { success: true, msg: "修改成功", changedRows: result.changedRows };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，修改失败" };
        }
        return result;
    },
    /*
     * 修改家校沟通访谈记录接口
     */
    async updateHomeSchoolInterview(uid, userType, interviewId, startTime, teacherId, interviewContent, isWarning) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.updateHomeSchoolInterview(interviewId, startTime, teacherId, interviewContent, isWarning);
            result = { success: true, msg: "修改成功", changedRows: result.changedRows };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，修改失败" };
        }
        return result;
    },
    /*
     * 删除日常访谈记录接口
     */
    async deleteDailyInterview(uid, userType, interviewId) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.deleteDailyInterview(interviewId);
            result = { success: true, msg: "删除成功" };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，删除失败" };
        }
        return result;
    },
    /*
     * 删除普查访谈记录接口
     */
    async deleteCensusInterview(uid, userType, interviewId) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.deleteCensusInterview(interviewId);
            result = { success: true, msg: "删除成功" };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，删除失败" };
        }
        return result;
    },
    /*
     * 删除家校沟通访谈记录接口
     */
    async deleteHomeSchoolInterview(uid, userType, interviewId) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新建访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.deleteHomeSchoolInterview(interviewId);
            result = { success: true, msg: "删除成功" };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，删除失败" };
        }
        return result;
    },
    /*
     * 查询某个日常访谈的详细信息
     */
    async getDailyInterviewInfo(uid, userType, interviewId) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '查看访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            let insertResult = await interviewDao.getDailyInterviewInfo(interviewId);
            for (let i = 0; i < insertResult.length; i++) {
                let imgId = insertResult[i].imgId;
                let photoUrl = "";
                if (imgId) {
                    let tmp = await fileDao.downloadPublicFile(imgId);
                    if (tmp.success) {
                        photoUrl = tmp.url;
                    }
                }
                insertResult[i].interviewPhoto = photoUrl;
            }
            result = { success: true, msg: "查看成功", result: insertResult };
        } catch (error) {
            result = { success: false, msg: "查看数据库时出错，查看失败" };
        }
        return result;
    },
    /*
     * 查询某个普查访谈的详细信息
     */
    async getCensusInterviewInfo(uid, userType, interviewId) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '查看访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await interviewDao.getCensusInterviewInfo(interviewId);
            result = { success: true, msg: "查看成功", result };
        } catch (error) {
            result = { success: false, msg: "查看数据库时出错，查看失败" };
        }
        return result;
    },
    /*
     * 查询某个家校沟通访谈的详细信息
     */
    async getHomeSchoolInterviewInfo(uid, userType, interviewId) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '查看访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            let insertResult = await interviewDao.getHomeSchoolInterviewInfo(interviewId);
            for (let i = 0; i < insertResult.length; i++) {
                let imgId = insertResult[i].imgId;
                let photoUrl = "";
                if (imgId) {
                    let tmp = await fileDao.downloadPublicFile(imgId);
                    if (tmp.success) {
                        photoUrl = tmp.url;
                    }
                }
                insertResult[i].interviewPhoto = photoUrl;
            }
            result = { success: true, msg: "查看成功", result: insertResult };
        } catch (error) {
            result = { success: false, msg: "查看数据库时出错，查看失败" };
        }
        return result;
    },
    /*
     * 查询某个学生的所有访谈记录
     */
    async getInterviewByStudent(uid, userType, studentId) {
        // 判断是否拥有新增日常访谈的权限
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '查看访谈记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '查看学生信息');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        let func = (userType == '辅导员' || userType == '学院书记') ? interviewDao.getInterviewByStudentOfSelfFaculty : interviewDao.getInterviewByStudent;
        try {
            result = await func(uid, studentId);
            result = { success: true, msg: "查询成功", result };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，查询失败" };
        }
        return result;
    },
    /*
     * 查询所有中心意见接口
     */
    async getAllCenterOpinion() {
        // 真正的执行逻辑
        try {
            result = await interviewDao.getAllCenterOpinion();
            result = { success: true, msg: "查询成功", result };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，查询失败" };
        }
        return result;
    }
};

module.exports = interviewService;